package com.onlineMIS.action.headQ.inventory;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.onlineMIS.ORM.DAO.headQ.custMgmt.HeadQCustDaoImpl;
import com.onlineMIS.ORM.entity.base.BaseOrder;
import com.onlineMIS.ORM.entity.headQ.SQLServer.ClientsMS;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ClientsMSVO;
import com.onlineMIS.ORM.entity.headQ.custMgmt.HeadQCust;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.DateUtils;
import com.onlineMIS.common.loggerLocal;
import com.opensymphony.xwork2.ActionContext;

import net.sf.json.JSONObject;

@Controller
public class BasicInforJSONAction extends BasicInforAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -9105960335932137110L;
	private String clientName;
	private String regionName;
	private int clientId;
	

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public int getClientId() {
		return clientId;
	}

	public void setClientId(int clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	private JSONObject jsonObject;

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	
	public JSONObject getJsonObject() {
		return jsonObject;
	}

	@Autowired
	private HeadQCustDaoImpl headQCustDaoImpl;
	
	
	
	
	/**
	 * once user input the client name in the field, and click the search button
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public String searchClients() throws IllegalAccessException, InvocationTargetException{
		loggerLocal.info(this.getClass() + ".searchClients()");
		
		List<ClientsMSVO> clients = basicInforService.getClientsByNameLike(clientName);
		Map<String,Object> jsonMap = new HashMap<String, Object>();

		jsonMap.put("clients", clients);
		
		try{
			   jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}	
		
		System.out.println(jsonObject);
		
		return SUCCESS;		
		
	}
	
	
	/**
	 * once user input the client name in the field, and click the search button
	 * @return
	 */
	public String searchClient(){
		
		loggerLocal.info(this.getClass() + ".searchClient()");
		
		ClientsMS client = basicInforService.getClientById(clientId);
		Map<String,Object> jsonMap = new HashMap<String, Object>();

		if (client == null)
		    jsonMap.put("client", "");
		else {
			jsonMap.put("client", client);
		}
		
		try{
			   jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}	
		
		System.out.println(jsonObject);
		
		return SUCCESS;		
		
	}
	
	/**
	 * once user input the client name in the field, and click the search button
	 * @return
	 */
	public String checkHeadCust(){
		loggerLocal.info(this.getClass() + ".checkHeadCust()");
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);


		HeadQCust  hCust = headQCustDaoImpl.get(clientId, true);
		if (hCust == null) {
			ClientsMS cMs = basicInforService.getClientById(clientId);
			HeadQCust headQCust = new HeadQCust();
			
			headQCust.setId(cMs.getClient_id());
			headQCust.setName(cMs.getName());
			headQCust.setPhone(cMs.getPhoneNumber());
			headQCust.setArea(cMs.getRegion().getName());
			headQCust.setAddress(cMs.getAddress());
			headQCust.setPinyin(cMs.getPinyin());
			headQCust.setStatus(0);
			headQCust.setComment("手动新增客户");
			headQCust.setCreationDate(Common_util.getToday());
			headQCust.setLastUpdateBy(loginUserInfor.getUser_name());
			headQCust.setLastUpdateTime(Common_util.getToday());
			headQCustDaoImpl.save(headQCust, true);
		}
		
		
		return SUCCESS;		
		
	}

}
